String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
String.prototype.ltrim = function() {
	return this.replace(/(^\s*)/g, "");
};
String.prototype.rtrim = function() {
	return this.replace(/(\s*$)/g, "");
};
String.prototype.endWith = function(str) {
	if (str == null || str == "" || this.length == 0 || str.length > this.length)
		return false;
	if (this.substring(this.length - str.length) == str)
		return true;
	else
		return false;
	return true;
};
String.prototype.startWith = function(str) {
	if (str == null || str == "" || this.length == 0 || str.length > this.length)
		return false;
	if (this.substr(0, str.length) == str)
		return true;
	else
		return false;
	return true;
};
// 替换全部
String.prototype.replaceAll = function(stringToFind, stringToReplace) {
	var temp = this;
	var index = temp.indexOf(stringToFind);
	var bi = -1;
	while (index != -1) {
		bi = index;
		temp = temp.replace(stringToFind, stringToReplace);
		index = temp.indexOf(stringToFind);
		if (bi >= index) {
			index = -1;
		}
	}
	return temp.toString();
};
var myUtil = {
		
	replaceAll : function(temp, a, b) {
		if (!temp) {
			return "";
		}
		var index = temp.indexOf(a);
		var bi = -1;
		while (index != -1) {
			bi = index;
			temp = temp.replace(a, b);
			index = temp.indexOf(a);
			if (bi >= index) {
				index = -1;
			}
		}
		return temp;
	},		
		
	// 获取菱形的坐标
	getPolygonPoints : function(b, c, d, e) {
		var _p = "";
		// left
		_p = _p + b;
		_p = _p + ",";
		_p = _p + (parseInt(c) + parseInt(e) / 2) + " ";
		// top
		_p = _p + (parseInt(b) + parseInt(d) / 2);
		_p = _p + ",";
		_p = _p + c + " ";
		// right
		_p = _p + (parseInt(b) + parseInt(d));
		_p = _p + ",";
		_p = _p + (parseInt(c) + parseInt(e) / 2) + " ";
		// bottom
		_p = _p + (parseInt(b) + parseInt(d) / 2);
		_p = _p + ",";
		_p = _p + (parseInt(c) + parseInt(e));
		return _p;
	},
	sort : function(array, getKey) {
		if (!array) {
			array = [];
		} else {
			var l = array.length;
			for (var i = 0; i < l; i++) {
				for (var j = 0; j < l - i - 1; j++) {
					if (getKey(array[j]) > getKey(array[j + 1])) {
						var temp = array[j];
						array[j] = array[j + 1];
						array[j + 1] = temp;
					}
				}
			}
		}
		return array;
	},
	isJsonString : function(str) {
		if (!str) {
			return false;
		}
		if (typeof str == 'object') {
			return false;
		}
		return (str.startWith("[") && str.endWith("]")) || (str.startWith("{") && str.endWith("}"));
	},
	isEmptyJson : function(obj) {
		for ( var k in obj) {
			return false;
		}
		return true;
	},
	isJson : function(json) {
		if (!json) {
			return false;
		}
		if (typeof json == 'object') {
			return true;
		}
	},
	stringToJson : function(s) {
		if (typeof s == "object") {
			return s;
		}
		if (!s) {
			return {};
		}
		try {
			return eval("(" + s + ")");
		} catch (e) {
			if (window.console) {
				window.console.log(s);
			}
			return {};
		}
	},
	jsonToString : function(o) {
		if (!o) {
			return "";
		}
		var arr = [];
		var fmt = function(s) {
			if (myUtil.isJson(s)) {
				if (s && s.length) {
					return myUtil.jsonArrayToString(s);
				} else {
					return myUtil.jsonToString(s);
				}
			}
			return /^(string)$/.test(typeof s) ? "'" + s + "'" : s;
		};
		for ( var i in o) {
			if (typeof o[i] != 'function') {
				arr.push("'" + i + "':" + fmt(o[i]));
			}
		}
		return '{' + arr.join(',') + '}';
	},
	jsonArrayToString : function(jsonArray) {
		if (!jsonArray) {
			return "";
		}
		if (typeof jsonArray != "object") {
			return jsonArray;
		}
		var l = jsonArray.length;
		if (!l) {
			return myUtil.jsonToString(jsonArray);
		}
		var str = "[";
		for (var i = 0; i < l; i++) {
			if (i > 0) {
				str = str + ",";
			}
			str = str + myUtil.jsonToString(jsonArray[i]);
		}
		str = str + "]";
		return str;
	},
	stopPropagation : function(e) {
		var evt = arguments.callee.caller.arguments[0] || window.event;// 消除浏览器差异
		if (!evt) {
			return;
		}
		if (evt.preventDefault) {
			// Firefox
			evt.preventDefault();
			evt.stopPropagation();
		} else {
			// IE
			evt.cancelBubble = true;
			evt.returnValue = false;
		}
	},
	min : function(a, b) {
		return a < b ? a : b;
	},
	max : function(a, b) {
		return a > b ? a : b;
	},
	loadDataSources : function(src, callback) {
		var data = myUtil.data[src];
		if (!data || data.length == 1) {
			myUtil.loadAjaxData(src, function(data, valueField, nameField) {
				callback(data, valueField, nameField);
				myUtil.data[src] = data;
			});
		} else {
			callback(data);
		}
	},
	loadAjaxData : function(url, callback) {
		$.ajax({
			cache : true,
			async : false,
			type : "GET",
			url : url,
			dataType : 'json',
			success : function(data) {
				if (!data || data.ret == 1) {
					alert(data.msg);
					callback([]);
				} else {
					callback(data.returns, data.valueField, data.nameField);
				}
			},
			error : function(e) {
				callback([]);
			}
		});
	},
	data : {}
};
